﻿using ImportMoudel;
using LightingServer.TemplateGenerator;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;

namespace LightingServer.TCP.Start
{
    public class PackageImportCommand : IServerCommand
    {
        public int CommandID
        {
            get { return (int)CommandType.PackageImport; }
        }

        public byte[] Excute(byte[] request)
        {
            ImportHandler handler = new ImportHandler();
            handler.Import(request);
            return new byte[] { (byte)1 };
        }
    }

    public class DownloadResourceCommand : IServerCommand
    {
        public int CommandID
        {
            get { return (int)CommandType.DownloadResource; }
        }

        public byte[] Excute(byte[] request)
        {
            return ResourceProvider.Res.ToByteArray();
        }
    }

    public class LoginCommand : IServerCommand
    {
        public int CommandID
        {
            get { return (int)CommandType.Login; }
        }

        public byte[] Excute(byte[] request)
        {
            User sourceUser = User.ParseFrom(request);
            User.Builder builder = new User.Builder();
            try
            {
                using (DbConnection conn = LightingServerContext.CreateConnection())
                {
                    using (DbCommand command = conn.CreateCommand())
                    {
                        conn.Open();
                        command.CommandText = "SELECT id, name, password, roleType FROM user WHERE name=@name AND password=@password;";

                        command.Parameters.AddRange(new MySqlParameter[]
                        {
                            new MySqlParameter()
                            {
                                ParameterName = "@name",
                                DbType = DbType.String,
                                Value = sourceUser.Name
                            },
                            new MySqlParameter()
                            {
                                ParameterName = "@password",
                                DbType = DbType.String,
                                Value = sourceUser.Password
                            },
                        });

                        using (var reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                builder.SetId(reader["id"].ToString());
                                builder.SetName(reader["name"].ToString());
                                builder.SetPassword(reader["password"].ToString());
                                builder.SetRoleType(uint.Parse(reader["roleType"].ToString()));
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return builder.Build().ToByteArray();
        }
    }

    public class LoadAllLablesCommand : IServerCommand
    {
        public int CommandID
        {
            get { return (int)CommandType.LoadAllLables; }
        }

        public byte[] Excute(byte[] request)
        {
            Res.Builder builder = new Res.Builder();
            try
            {
                LightingServerContext.WriteLine("LoadAllLables");
                using (DbConnection conn = LightingServerContext.CreateConnection())
                {
                    using (DbCommand command = conn.CreateCommand())
                    {
                        conn.Open();
                        command.CommandText = "SELECT DISTINCT lable FROM lighting ORDER BY lable;";

                        using (var reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                string lable = reader["lable"] != null ? reader["lable"].ToString().Trim() : null;
                                if (!string.IsNullOrEmpty(lable))
                                {
                                    ResItem.Builder itemBuilder = new ResItem.Builder();
                                    itemBuilder.SetItemName(lable);
                                    builder.Res_List.Add(itemBuilder.Build());
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return builder.Build().ToByteArray();
        }
    }

    public class QueryLevel2Command : IServerCommand
    {
        public int CommandID
        {
            get { return (int)CommandType.QueryLevel2; }
        }

        public byte[] Excute(byte[] request)
        {
            QueryService service = new QueryService();
            Lightings lightings = service.QueryLevel2(QueryParameter2.ParseFrom(request));
            return lightings.ToByteArray();
        }
    }

    public class QueryLevel3Command : IServerCommand
    {
        public int CommandID
        {
            get { return (int)CommandType.QueryLevel3; }
        }

        public byte[] Excute(byte[] request)
        {
            QueryService service = new QueryService();
            Lightings lightings = service.QueryLevel3(QueryParameter3.ParseFrom(request));
            return lightings.ToByteArray();
        }
    }

    public class QueryLevel4Command : IServerCommand
    {
        public int CommandID
        {
            get { return (int)CommandType.QueryLevel4; }
        }

        public byte[] Excute(byte[] request)
        {
            QueryService service = new QueryService();
            Lighting lighting = service.QueryLevel4(Encoding.UTF8.GetString(request));
            return lighting.ToByteArray();
        }
    }

    public class QueryLevel5Command : IServerCommand
    {
        public int CommandID
        {
            get { return (int)CommandType.QueryLevel5; }
        }

        public byte[] Excute(byte[] request)
        {
            QueryService service = new QueryService();
            Lighting lighting = service.QueryLevel5(Encoding.UTF8.GetString(request));
            return lighting.ToByteArray();
        }
    }

    public class QueryCommand : IServerCommand
    {
        public int CommandID
        {
            get { return (int)CommandType.Query; }
        }

        public byte[] Excute(byte[] request)
        {
            QueryService service = new QueryService();
            Lightings lightings = service.Query(LightingQuery.ParseFrom(request));
            return lightings.ToByteArray();
        }
    }

    public class GetUsersCommand : IServerCommand
    {
        public int CommandID
        {
            get { return (int)CommandType.GetUsers; }
        }

        public byte[] Excute(byte[] request)
        {
            QueryService service = new QueryService();
            Users users = service.GetUsers();
            return users.ToByteArray();
        }
    }

    public class AddUserCommand : IServerCommand
    {
        public int CommandID
        {
            get { return (int)CommandType.AddUser; }
        }

        public byte[] Excute(byte[] request)
        {
            QueryService service = new QueryService();
            return new byte[] { (byte)(service.AddUser(User.ParseFrom(request)) ? 1 : 0) };
        }
    }

    public class DeleteUserCommand : IServerCommand
    {
        public int CommandID
        {
            get { return (int)CommandType.DeleteUser; }
        }

        public byte[] Excute(byte[] request)
        {
            QueryService service = new QueryService();
            return new byte[] { (byte)(service.DeleteUser(Encoding.UTF8.GetString(request)) ? 1 : 0) };
        }
    }

    public class UpdateUserCommand : IServerCommand
    {
        public int CommandID
        {
            get { return (int)CommandType.UpdateUser; }
        }

        public byte[] Excute(byte[] request)
        {
            QueryService service = new QueryService();
            return new byte[] { (byte)(service.UpdateUser(User.ParseFrom(request)) ? 1 : 0) };
        }
    }

    public class RelatedProductCommand : IServerCommand
    {
        public int CommandID
        {
            get { return (int)CommandType.RelatedProduct; }
        }

        public byte[] Excute(byte[] request)
        {

            QueryService service = new QueryService();
            Lightings lightings = service.QueryRelatedProduct(RelatedProduct.ParseFrom(request));
            return lightings.ToByteArray();
        }
    }

    public class AddLightingCommand : IServerCommand
    {
        public int CommandID
        {
            get { return (int)CommandType.AddLighting; }
        }

        public byte[] Excute(byte[] request)
        {
            QueryService service = new QueryService();
            return new byte[] { (byte)(service.AddLighting(Lighting.ParseFrom(request)) ? 1 : 0) };
        }
    }

    public class EditLightingCommand : IServerCommand
    {
        public int CommandID
        {
            get { return (int)CommandType.EditLighting; }
        }

        public byte[] Excute(byte[] request)
        {
            QueryService service = new QueryService();
            return new byte[] { (byte)(service.EditLighting(Lighting.ParseFrom(request)) ? 1 : 0) };
        }
    }

    public class DeleteLightingCommand : IServerCommand
    {
        public int CommandID
        {
            get { return (int)CommandType.DeleteLighting; }
        }

        public byte[] Excute(byte[] request)
        {
            QueryService service = new QueryService();
            return new byte[] { (byte)(service.DeleteLighting(Lighting.ParseFrom(request)) ? 1 : 0) };
        }
    }

    public class QueryExportExcel : IServerCommand
    {
        public int CommandID
        {
            get { return (int)CommandType.QueryExportExcel; }
        }

        public byte[] Excute(byte[] request)
        {
            QueryService service = new QueryService();
            Lightings lightings = service.Query(LightingQuery.ParseFrom(request));

            ListGenerator generator = new ListGenerator();
            return generator.Generate(lightings);
        }
    }
    public class QueryExportPdf : IServerCommand
    {
        public int CommandID
        {
            get { return (int)CommandType.QueryExportPdf; }
        }

        public byte[] Excute(byte[] request)
        {
            QueryService service = new QueryService();
            Lightings lightings = service.Query(LightingQuery.ParseFrom(request));

            ListGenerator generator = new ListGenerator();
            return generator.Generate(lightings, true);
        }
    }
    public class QueryLevel3ExportExcel : IServerCommand
    {
        public int CommandID
        {
            get { return (int)CommandType.QueryLevel3ExportExcel; }
        }

        public byte[] Excute(byte[] request)
        {
            QueryService service = new QueryService();
            Lightings lightings = service.QueryLevel3(QueryParameter3.ParseFrom(request));

            ListGenerator generator = new ListGenerator();
            return generator.Generate(lightings);
        }
    }
    public class QueryLevel3ExportPdf : IServerCommand
    {
        public int CommandID
        {
            get { return (int)CommandType.QueryLevel3ExportPdf; }
        }

        public byte[] Excute(byte[] request)
        {
            QueryService service = new QueryService();
            Lightings lightings = service.QueryLevel3(QueryParameter3.ParseFrom(request));

            ListGenerator generator = new ListGenerator();
            return generator.Generate(lightings);
        }
    }
    public class QueryLevel4ExportExcel : IServerCommand
    {
        public int CommandID
        {
            get { return (int)CommandType.QueryLevel4ExportExcel; }
        }

        public byte[] Excute(byte[] request)
        {
            QueryService service = new QueryService();
            Lighting lighting = service.QueryLevel4(Encoding.UTF8.GetString(request));

            SpecGenerator generator = new SpecGenerator();
            return generator.Generate(lighting);
        }
    }
    public class QueryLevel4ExportPdf : IServerCommand
    {
        public int CommandID
        {
            get { return (int)CommandType.QueryLevel4ExportPdf; }
        }

        public byte[] Excute(byte[] request)
        {
            QueryService service = new QueryService();
            Lighting lighting = service.QueryLevel4(Encoding.UTF8.GetString(request));

            SpecGenerator generator = new SpecGenerator();
            return generator.Generate(lighting, true);
        }
    }
    public class QueryLevel5ExportExcel : IServerCommand
    {
        public int CommandID
        {
            get { return (int)CommandType.QueryLevel5ExportExcel; }
        }

        public byte[] Excute(byte[] request)
        {
            QueryService service = new QueryService();
            Lighting lighting = service.QueryLevel5(Encoding.UTF8.GetString(request));

            SpecAllGenerator generator = new SpecAllGenerator();
            return generator.Generate(lighting);
        }
    }
    public class QueryLevel5ExportPdf : IServerCommand
    {
        public int CommandID
        {
            get { return (int)CommandType.QueryLevel5ExportPdf; }
        }

        public byte[] Excute(byte[] request)
        {
            QueryService service = new QueryService();
            Lighting lighting = service.QueryLevel5(Encoding.UTF8.GetString(request));

            SpecAllGenerator generator = new SpecAllGenerator();
            return generator.Generate(lighting, true);
        }
    }
}
